var websocket=require("../websocket");
var protocol=require("../protocol");
var Cmd=require("../Cmd");


    export default class mainView extends Laya.Script {

    constructor() {
        super();

        
       // Laya.MiniAdpter.nativefiles = [
       //     'res/atlas'
		//];


		//创建场景
		this.scene = new Laya.Scene3D();
		Laya.stage.addChild(this.scene);
		
		//创建相机
		let camera = new Laya.Camera(0, 0.1, 100);
		this.scene.addChild(camera);
		camera.transform.translate(new Laya.Vector3(0, 0.5, 1));
		camera.transform.rotate(new Laya.Vector3(-15, 0, 0), true, false);
	
		
		//添加光照
		let directionLight = new Laya.DirectionLight();
		this.scene.addChild(directionLight);
		directionLight.color = new Laya.Vector3(1, 1, 1);
        directionLight.transform.rotate(new Laya.Vector3( -3.14 / 3, 0, 0));
        

        	//预加载所有资源
		let resource = [
			"res/models/cars/ae86/car.lh" ];
        Laya.loader.create(resource, Laya.Handler.create(this, this.onPreLoadFinish));
        Laya.URL.basePath = "http://47.100.80.231:6080/";

        //连接服务器
        //websocket.connect("ws://47.100.80.231:6081/ws", protocol.PROTO_JSON);
        

        websocket.connect("ws://127.0.0.1:6081/ws", protocol.PROTO_JSON);
        
        var services={};
        services[Cmd.Stype.GameSys]=this.on_gameSys_server_callback.bind(this);
        websocket.register_serivces_handler(services);


    }

    on_gameSys_server_callback(stype,ctype,body){
        switch(ctype)
        {
            case Cmd.Request.GameSys.USER_LOGIN:
            if (body.Respones==Cmd.Respones.OK)
                {
                    //获取游戏数据
                    console.log(body.gold,body.diamond);
                }
            break;
        }
    }

    onPreLoadFinish() {
        let layaMonkeyParent = this.scene.addChild(Laya.Loader.getRes("res/models/cars/ae86/car.lh"));


        websocket.send_cmd(Cmd.Stype.GameSys,Cmd.Request.GameSys.USER_LOGIN,"小飞");
    }

    onEnable() {

        let exportJson = {};
        let sysInfo = wx.getSystemInfoSync();
        let width = sysInfo.screenWidth;
        let height = sysInfo.screenHeight;

        wx.login({
            success: res => {
                console.log('玩家登陆', res);
                
                //可以在这里得
                websocket.send_cmd(Cmd.Stype.GameSys,Cmd.Request.GameSys.USER_LOGIN,body);
            }
        });

        wx.getSetting({
            success(res) {
                console.log(res.authSetting);
                if (res.authSetting["scope.userInfo"]) {
                    console.log("用户已授权");
                    wx.getUserInfo({
                        success(res) {
                            console.log(res);
                            exportJson.userInfo = res.userInfo;
                            //此时可进行登录操作
                        }
                    });
                } else {
                    console.log("用户未授权");
                    let button = wx.createUserInfoButton({
                        type: 'text',
                        text: '',
                        style: {
                            left: 0,
                            top: 0,
                            width: width,
                            height: height,
                            backgroundColor: '#00000000',//最后两位为透明度
                            color: '#ffffff',
                            fontSize: 20,
                            textAlign: "center",
                            lineHeight: height,
                        }
                    });
                    button.onTap((res) => {
                        if (res.userInfo) {
                            console.log("用户授权:", res);
                            exportJson.userInfo = res.userInfo;
                            此时可进行登录操作
                            button.destroy();
                        } else {
                            console.log("用户拒绝授权:", res);
                        }
                    });
                }
            }
        });
    }

    onDisable() {
    }
}